module Dictl ( Dict,
	    Word,
	    Def,
	    dict_empty,
	    dict_add,
	    dict_exist,
	    dict_del,
	    dict_length,
	    dict_toDefs )
where
import Data
import Key
import ListAssoco

--las palabras son key y las def son datas

data Dict = Dict Int (ListAssoc Key Data)
	deriving Show
	
type Word = String
type Def = String

dict_empty :: Dict
dict_empty = Dict 0 la_empty

dict_exist :: Dict -> Word -> Bool
dict_exist (Dict n lstAsc) wrd = case la_search lstAsc (key_fromString wrd) of
					Nothing -> False
					Just _ -> True
				
dict_add :: Dict -> Word -> Def -> Dict
dict_add (Dict n lstAsc) wrd def = case la_search lstAsc (key_fromString wrd) of
					Nothing -> Dict (n+1) (la_add lstAsc (key_fromString wrd) (data_fromString def))
					Just _ -> Dict n lstAsc

dict_search :: Dict -> Word -> Maybe Def
dict_search (Dict n lstAsc) wrd = case la_search lstAsc (key_fromString wrd) of
					Nothing -> Nothing
					Just m -> Just (data_toString m)

dict_del :: Dict -> Word -> Dict
dict_del (Dict n lstAsc) wrd = case la_search lstAsc (key_fromString wrd) of
				Nothing -> Dict n lstAsc
				Just m -> Dict (n-1) (la_del lstAsc (key_fromString wrd))

dict_length :: Dict -> Int
dict_length (Dict n lstAsc) = n

trans :: [(Key,Data)] -> [(Word, Def)]
trans [] = []
trans ((x,y):xs) = (key_toString x, data_toString y) : trans xs

dict_toDefs :: Dict -> [(Word,Def)]
dict_toDefs (Dict n lstAsc) = trans (la_toListPair lstAsc)




